package io.renren.softpark.service.impl;

import com.yunlong.common.utils.PageUtils;
import com.yunlong.common.utils.Query;
import io.renren.softpark.dao.ColumnDao;
import io.renren.softpark.dao.CommentDao;
import io.renren.softpark.dto.VerifyCommentDto;
import io.renren.softpark.entity.ColumnEntity;
import io.renren.softpark.entity.CommentEntity;
import io.renren.softpark.service.CommentService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;



@Service("commentService")
public class CommentServiceImpl extends ServiceImpl<CommentDao, CommentEntity> implements CommentService {

    @Autowired
    ColumnDao columnDao;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CommentEntity> page = this.page(
                new Query<CommentEntity>().getPage(params),
                new QueryWrapper<CommentEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<VerifyCommentDto> verifyComment(Map<String, Object> params) {
        IPage<CommentEntity> page = this.page(
                new Query<CommentEntity>().getPage(params),
                new QueryWrapper<CommentEntity>()
        );
        List<CommentEntity> records = page.getRecords();
        List<ColumnEntity> columnEntities = columnDao.selectList(new QueryWrapper<>());
        List<VerifyCommentDto> collect1 = records.stream().map((r) -> {
            VerifyCommentDto v = new VerifyCommentDto();
            BeanUtils.copyProperties(r, v);
            List<ColumnEntity> collect = columnEntities.stream().filter((c) ->
                    c.getColumnId().equals(r.getColumnId())
            ).collect(Collectors.toList());
            v.setColumnName(collect.get(0).getColumnName());
            return v;
        }).collect(Collectors.toList());
        return collect1;
    }

}